package com.youfang.sixthmedia.ui.detail;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gzit.common.async.AsynCallback;
import com.gzit.common.async.LoadingCallback;
import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.youfang.sixthmedia.R;
import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.controller.HeaderController;
import com.youfang.sixthmedia.controller.HeaderController.HeaderAdapter;
import com.youfang.sixthmedia.message.AddCommentRequest;
import com.youfang.sixthmedia.message.AddCommentResponse;
import com.youfang.sixthmedia.message.GetCommentRequest;
import com.youfang.sixthmedia.message.GetCommentResponse;
import com.youfang.sixthmedia.message.model.DetailBuilding;
import com.youfang.sixthmedia.message.model.DetailComment;
import com.youfang.sixthmedia.net.Net;
import com.youfang.sixthmedia.net.Response;
import com.youfang.sixthmedia.widget.GZITPullToRefreshListView;
import com.youfang.sixthmedia.widget.GZITPullToRefreshListView.OnBottomListener;
import com.youfang.sixthmedia.widget.GZITPullToRefreshListView.OnRefreshListener;

public class DetailCommentActivity extends Activity {

	private int page = 1;

	private HeaderController controller = new HeaderController();
	private GZITPullToRefreshListView listview;
	private DetailCommentAdapter adapter;

	// 网友楼盘评论的控制器
	public static class DetailCommentAdapter extends
			HeaderAdapter<DetailComment> {

		public DetailCommentAdapter(Context context) {
			super(context, R.layout.detail_comment_item);
		}

		// 直接返回postion有没有什么问题？
		public long getItemId(int position) {
			return position;
		}

		@Override
		protected void render(DetailComment item, View view) {

			ImageView logo = (ImageView) view.findViewById(R.id.logo);
			logo.setImageBitmap(item.logoBitmap);

			// other info
			TextView nickname = (TextView) view.findViewById(R.id.nickname);
			AndroidUtils.boldText(nickname);
			nickname.setText(item.nickname);
			AndroidUtils.boldText(nickname);

			TextView commentcontent = (TextView) view
					.findViewById(R.id.commentcontent);
			commentcontent.setText(item.commentcontent);

			TextView createtime = (TextView) view.findViewById(R.id.createtime);
			createtime.setText(com.gzit.utils.DateUtil
					.dateTime2String(new Date(item.createtime)));

			// TD: 注册事件...

		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.detail_comment_activity);

		// building detail is mandatory...
		// in case no building detail found,activity ui should not initialize
		DetailBuilding detail = YoufangContext.getCurrentBuilding();
		if (detail == null) {
			return;
		}

		this.listview = (GZITPullToRefreshListView) this
				.findViewById(R.id.listview);

		this.adapter = new DetailCommentAdapter(this);
		this.listview.setAdapter(adapter);

		this.controller.setup(listview, adapter);

		this.listview.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {

				remoteRefresh();

			}
		});
		this.listview.setOnBottomListener(new OnBottomListener() {

			@Override
			public void onBottomClick() {
				remoteMore();
			}

			@Override
			public void onBottom() {

			}
		});

		// test
		// this.dataItem = new DetailBuilding();
		// dataItem.buildingname = "中州中央公园";
		// dataItem.id = "5016";

		((TextView) this.findViewById(R.id.title)).setText(detail.buildingname);

		// response to comment btn event
		this.findViewById(R.id.comment_btn).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						remoteAddComment();
					}
				});

		remoteRefresh();
	}

	public void remoteAddComment() {

		final DetailBuilding detail = YoufangContext.getCurrentBuilding();
		if (detail == null) {
			return;
		}

		String content = ((EditText) this.findViewById(R.id.comment)).getText()
				.toString();
		if (StringUtils.isBlank(content)) {
			AndroidUtils.toastShort("评论内容为空，评论失败");
			return;
		}

		AddCommentRequest req = new AddCommentRequest();
		req.setHouseid(detail.id);
		req.setContent(content);

		Net.doHttpRemote(this, req, new AddCommentResponse(),
				new AsynCallback<Response>() {

					@Override
					public void callback(Response r) {
						if (!r.isOk()) {
							AndroidUtils.toastShort("评论发表失败");
							return;
						}

						AndroidUtils.Keyboard
								.hideKeyboard(DetailCommentActivity.this);

						// successfully commented ,then refresh list...
						remoteRefresh();

					}
				});

	}

	public void remoteRefresh() {

		final DetailBuilding detail = YoufangContext.getCurrentBuilding();
		if (detail == null) {
			return;
		}

		// 重置页码
		page = 1;
		GetCommentRequest req = new GetCommentRequest();
		req.setHouseid(detail.id);
		req.setPage(page);
		Net.doHttpRemote(this, req, new GetCommentResponse(),
				new AsynCallback<Response>() {

					@Override
					public void onFinish(Response r) {
						super.onFinish(r);
						listview.onRefreshComplete();
					}

					@Override
					public void callback(Response r) {
						if (!r.isOk()) {
							return;
						}

						GetCommentResponse resp = (GetCommentResponse) r;
						List<DetailComment> dataList = resp.getDataList();

						adapter.reload(dataList);
						listview.setSelection(1);
						controller.onRefresh(dataList);

						// 页码递增
						page++;

					}
				}, LoadingCallback.EMPTY);

	}

	public void remoteMore() {

		final DetailBuilding detail = YoufangContext.getCurrentBuilding();
		if (detail == null) {
			return;
		}

		GetCommentRequest req = new GetCommentRequest();
		req.setHouseid(detail.id);
		req.setPage(page);

		Net.doHttpRemote(this, req, new GetCommentResponse(),
				new AsynCallback<Response>() {

					@Override
					public void onFinish(Response r) {
						super.onFinish(r);
						listview.onBottomFinish();
					}

					@Override
					public void callback(Response r) {
						if (!r.isOk()) {
							return;
						}

						GetCommentResponse resp = (GetCommentResponse) r;
						List<DetailComment> dataList = resp.getDataList();

						adapter.addAll(dataList);
						controller.onMore(dataList);

						// 页码递增
						page++;

					}
				}, LoadingCallback.EMPTY);

	}

	@Override
	protected void onStop() {
		super.onStop();
		controller.onStop();
	}

}
